package com.beijiteshop.shop.utils

import android.content.Context
import androidx.core.content.ContextCompat
import com.beijiteshop.shop.R
import com.beijiteshop.shop.view.SelectBigPagerTitleView
import net.lucode.hackware.magicindicator.MagicIndicator
import net.lucode.hackware.magicindicator.buildins.commonnavigator.CommonNavigator
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.CommonNavigatorAdapter
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerIndicator
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerTitleView
import net.lucode.hackware.magicindicator.buildins.commonnavigator.indicators.LinePagerIndicator

/**
 * @Author LiaoZhongKai
 * @Date 2021/3/4 10:13
 * @Description 封装MagicIndicator的创建
 */
object TabCreateUtil {

    /**
     * 创建Indicator
     * @param titles 标题集合
     * @param listener tab点击事件
     */
    fun create(context: Context,indicator: MagicIndicator,titles: List<String>,listener: (index: Int) -> Unit){
        val commonNavigator = CommonNavigator(context)
        commonNavigator.adapter = object : CommonNavigatorAdapter(){
            override fun getTitleView(context: Context, index: Int): IPagerTitleView {
                return SelectBigPagerTitleView(context).apply {
                    normalColor = ContextCompat.getColor(context,R.color.text_dark)
                    selectedColor = ContextCompat.getColor(context,R.color.text_dark)
                    text = titles[index]

                    setOnClickListener {
                        commonNavigator.onPageSelected(index)
                        listener.invoke(index)
                    }
                }
            }

            override fun getCount(): Int {
                return titles.size
            }

            override fun getIndicator(context: Context): IPagerIndicator {
                return LinePagerIndicator(context).apply {
                    mode = LinePagerIndicator.MODE_WRAP_CONTENT
                    setColors(ContextCompat.getColor(context, R.color.color_primary))
                    roundRadius = 3f
                }
            }
        }
        commonNavigator.isAdjustMode = false
        indicator.navigator = commonNavigator
    }
}